/**$Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *   Copyright (C) 2008 - 2012 xingsheng.zhu@gmail.com All rights reserved.
 */
package com.blazemina.config;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSetBase;

/**
 * @author JackChu
 * @version 1.0.0 2012-1-4 下午4:58:56
 */
public class ConfigRuleSet extends RuleSetBase {

	@Override
	public void addRuleInstances(Digester digester) {
		ClassLoader cl = digester.getClassLoader();

		digester.addObjectCreate("blazemina-config", BlazeminaConfig.class);

		parseIoService(digester);

		parseSocketOption(digester);
		
		parseProtocols(digester);
	}

	private void parseIoService(Digester digester) {
		digester.addObjectCreate("blazemina-config/io-service", IoService.class);

		digester.addBeanPropertySetter(
				"blazemina-config/io-service/socket-threads", "socketThreads");

		digester.addObjectCreate(
				"blazemina-config/io-service/service-acceptor",
				ServiceAcceptor.class);
		digester.addBeanPropertySetter(
				"blazemina-config/io-service/service-acceptor/service-name",
				"serviceName");
		digester.addBeanPropertySetter(
				"blazemina-config/io-service/service-acceptor/local-host",
				"localHost");
		digester.addBeanPropertySetter(
				"blazemina-config/io-service/service-acceptor/local-port",
				"localPort");

		digester.addObjectCreate(
				"blazemina-config/io-service/service-acceptor/service-filters/filter",
				ServiceFilter.class);
		digester.addSetProperties(
				"blazemina-config/io-service/service-acceptor/service-filters/filter",
				"name", "name");
		digester.addSetProperties(
				"blazemina-config/io-service/service-acceptor/service-filters/filter",
				"type", "type");
		digester.addSetProperties(
				"blazemina-config/io-service/service-acceptor/service-filters/filter",
				"class", "className");

		digester.addSetNext(
				"blazemina-config/io-service/service-acceptor/service-filters/filter",
				"addFilter", ServiceFilter.class.getName());

		digester.addBeanPropertySetter(
				"blazemina-config/io-service/service-acceptor/service-handle",
				"serviceHandle");
		digester.addBeanPropertySetter(
				"blazemina-config/io-service/service-acceptor/session-handle",
				"sessionHandle");
		digester.addBeanPropertySetter(
				"blazemina-config/io-service/service-acceptor/command-factory",
				"commandFactory");
		digester.addBeanPropertySetter(
				"blazemina-config/io-service/service-acceptor/request-processor",
				"requestProcessor");

		digester.addSetNext("blazemina-config/io-service/service-acceptor",
				"setServiceAcceptor", ServiceAcceptor.class.getName());

		digester.addSetNext("blazemina-config/io-service", "setIoService",
				IoService.class.getName());
	}

	private void parseSocketOption(Digester digester) {
		digester.addObjectCreate("blazemina-config/socket-option",
				SocketOption.class);

		digester.addBeanPropertySetter(
				"blazemina-config/socket-option/socketReceiveBuffer",
				"socketReceiveBuffer");
		digester.addBeanPropertySetter(
				"blazemina-config/socket-option/socketSendBuffer",
				"socketSendBuffer");
		digester.addBeanPropertySetter(
				"blazemina-config/socket-option/keepAlive", "keepAlive");
		digester.addBeanPropertySetter(
				"blazemina-config/socket-option/tcpNoDelay", "tcpNoDelay");

		digester.addObjectCreate("blazemina-config/socket-option/idleTime",
				IdleTime.class);
		digester.addBeanPropertySetter("blazemina-config/socket-option/idleTime/state");
		digester.addBeanPropertySetter("blazemina-config/socket-option/idleTime/timeoutSeconds");

		digester.addSetNext("blazemina-config/socket-option/idleTime",
				"setIdleTime", IdleTime.class.getName());

		digester.addSetNext("blazemina-config/socket-option",
				"setSocketOption", SocketOption.class.getName());
	}

	private void parseProtocols(Digester digester) {
		digester.addObjectCreate("blazemina-config/protocols", Protocols.class);

		digester.addBeanPropertySetter("blazemina-config/protocols/commandFactory");

		digester.addObjectCreate(
				"blazemina-config/protocols/command-mappings/command",
				ActionConfig.class);
		digester.addSetProperties(
				"blazemina-config/protocols/command-mappings/command", "name",
				"name");
		digester.addSetProperties(
				"blazemina-config/protocols/command-mappings/command",
				"className", "className");
		digester.addSetProperties(
				"blazemina-config/protocols/command-mappings/command",
				"methodName", "methodName");

		digester.addSetNext(
				"blazemina-config/protocols/command-mappings/command",
				"addCommandConfig", ActionConfig.class.getName());

		digester.addSetNext("blazemina-config/protocols", "setProtocols",
				Protocols.class.getName());
	}

}
